package oh.yeah.monkey;

import java.lang.ref.WeakReference;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import oh.yeah.monkey.Service;
import oh.yeah.monkey.Constant;
import oh.yeah.monkey.Network;
import android.app.*;
import android.*;
import android.webkit.*;
import android.widget.*;
import android.view.*;
import android.content.*;
import android.net.*;
import android.os.*;

public class MainActivity extends Activity
{
	private WebView webView;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
		
		ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
		NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		if (mWifi.isConnected()) {
		}else{
		 Dialog alertDialog = new AlertDialog.Builder(this)
		  .setTitle("哦不").setMessage("没有连接到WiFi网路\n您的选择？")
		  .setPositiveButton("连接WiFi", new DialogInterface.OnClickListener() {
		   @Override
		   public void onClick(DialogInterface arg0, int arg1) {
			Intent intent = new Intent();
			intent.setAction("android.net.wifi.PICK_WIFI_NETWORK");
			intent.putExtra("extra_prefs_show_button_bar", true);
			intent.putExtra("wifi_enable_next_on_connect", true);
			startActivity(intent);
		   }
		  })
		  .setNeutralButton("热点模式", new DialogInterface.OnClickListener() {
		   @Override
		   public void onClick(DialogInterface arg0, int arg2) {
			Toast.makeText(MainActivity.this, "请自己去设置中开启热点哦-.-已切换为热点模式", 0).show();
		   }
		  })
		  .create();
		 alertDialog.show();
		}
		
        startService(new Intent(getApplicationContext(), Service.class));
        String ip = Network.getLocalIp(getApplicationContext());
		
		TextView textView = (TextView) findViewById(R.id.mainTextView1);
		
		webView = (WebView) findViewById(R.id.webView);
		webView.setBackgroundColor(0);
	 if (mWifi.isConnected()) {
	  textView.setText("扫码吧，骚年\n当然"+ip+":7999也是滋磁的");
		webView.loadUrl("http://qr.liantu.com/api.php?w=200&h=200&text=http://" + ip +":"+ Constant.Config.PORT + Constant.Config.Web_Root);
		}else{
		 textView.setText("扫码吧，骚年\n当然192.168.43.1:7999也是滋磁的");
		 webView.loadUrl("http://qr.liantu.com/api.php?w=200&h=200&text=http://192.168.43.1:"+ Constant.Config.PORT + Constant.Config.Web_Root);
		}
		webView.setWebChromeClient(new WebChromeClient() {
				private ProgressBar Progress;
				@Override
				public void onProgressChanged(WebView view, int newProgress) {
					Progress = (ProgressBar)findViewById(R.id.mainProgressBar1);
					Progress.setProgress(newProgress);
					super.onProgressChanged(view, newProgress);
				}
			});
}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		Dialog alertDialog = new AlertDialog.Builder(this)
			.setTitle("退出").setMessage("是否关闭猴子分享？")
			.setPositiveButton("是", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface arg0, int arg1) {
				stopService(new Intent(getApplicationContext(), Service.class));
				MainActivity.this.finish();
				}
			})
		 .setNeutralButton("关于", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface arg0, int arg2) {
					Dialog alertDialog = new AlertDialog.Builder(MainActivity.this)
						.setTitle("关于").setMessage("猴子分享 1.1.6\nQAQ紧急修复一处错误\n修复bug\n你问我为什么软件这么小？\n我只能说一句无可奉告\n\n使用的开源库:\nhttpcore-Apache\nGoogle v7库(已经去掉)\n\n顺便安利一本书《提问的智慧》")
						.setPositiveButton("提问的智慧", new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface arg0, int arg1) {
								Intent intent = new Intent();        
								intent.setAction("android.intent.action.VIEW");    
								Uri content_url = Uri.parse("http://www.jianshu.com/p/60dd8e9cd12f");   
								intent.setData(content_url);  
								startActivity(intent);
							}
						})
						 
						.setNeutralButton("挑逗豆豆", new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface arg0, int arg2) {
								String url="mqqwpa://im/chat?chat_type=wpa&uin=1400157412";
								startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); 
								}
							}) .create();
					alertDialog.show();
				}
			})
			.create();
        alertDialog.show();
		return true;
	}


    private static class MainHandler extends Handler
    {
        private WeakReference<MainActivity> weakReference;
        public MainHandler(MainActivity activity)
        {
            weakReference = new WeakReference<MainActivity>(activity);
        }
    }
}
